package com.tanger.common.result;

import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description: 响应结果
 * @author Hchenbin.
 * @date 2022年3月4日 上午9:27:44
 * @ClassName: ActionResult
 * 
 * @param <T>
 */
public class ActionResult<T> implements Serializable {

	private static final long serialVersionUID = -8041820346732720877L;
	private static final String resultMsg = "success"; 
	private int status;// 状态编码
	private String message;// 消息
	private T data;

	public ActionResult() {
		super();
	}

	public ActionResult(int status, T data, String message) {
		super();
		this.status = status;
		this.message = message;
		this.data = data;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	/**
	 *
	 * @Description: 成功:返回字符串结果
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:29:59
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultSuccess() {
		ActionResult<T> listResult = new ActionResult<>();
		try {
			listResult.setData(null);
			listResult.setStatus(HttpStatus.OK.value());
			listResult.setMessage(resultMsg);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
		return listResult;
	}

	/**
	 *
	 * @Description: 成功:返回字符串结果
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:29:59
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultSuccess(Object obj) {
		ActionResult listResult = new ActionResult<>();
		try {
			listResult.setData(obj);
			listResult.setStatus(HttpStatus.OK.value());
			listResult.setMessage(resultMsg);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
		return listResult;
	}


	/**
	 *
	 * @Description: 成功:返回字符串结果
	 * @param data
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:29:59
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultSuccess(String data) {
		ActionResult listResult = new ActionResult<>();
		try {
			listResult.setData(data);
			listResult.setStatus(HttpStatus.OK.value());
			listResult.setMessage(resultMsg);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
		return listResult;
	}
	
	/**
	 * 
	 * @Description: 成功:返回Map结果
	 * @param data
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:32:33
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultSuccess(Map<String, Object> data) {
		ActionResult listResult = new ActionResult<>();
		try {
			listResult.setData(data);
			listResult.setStatus(HttpStatus.OK.value());
			listResult.setMessage(resultMsg);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
		return listResult;
	}

	/**
	 *
	 * @Description: 成功:返回List结果
	 * @param data
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月3日 上午9:35:04
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultSuccess(List<?> data) {
		ActionResult listResult = new ActionResult<>();
		try {
			listResult.setData(data);
			listResult.setStatus(HttpStatus.OK.value());
			listResult.setMessage(resultMsg);
		} catch (Exception e) {
			return ActionResult.buildResultError(e.getMessage());
		}
		return listResult;
	}

	/**
	 * 
	 * @Description: 错误:返回错误信息及字符串数据
	 * @param message
	 * @param data
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:40:24
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultError(String message,String data) {
		ActionResult listResult = new ActionResult<>();
		listResult.setData(data);
		listResult.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		listResult.setMessage(message);
		return listResult;
	}

	/**
	 *
	 * @Description: 错误:返回错误信息及字符串数据
	 * @param message
	 * @param data
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:40:24
	 * @version V1.0
	 */
	public static <T> ActionResult<T> buildResultError(int code, String message,String data) {
		ActionResult listResult = new ActionResult<>();
		listResult.setData(data);
		listResult.setStatus(code);
		listResult.setMessage(message);
		return listResult;
	}
	
	/**
	 * 
	 * @Description: 错误:返回错误信息
	 * @param message
	 * @return
	 * @author Hchenbin.
	 * @date 2022年3月4日 上午9:42:19 
	 * @version V1.0
	 */
	public static ActionResult buildResultError(String message) {
		ActionResult listResult = new ActionResult<>();
		listResult.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		listResult.setMessage(message);
		return listResult;
	}

	/**
	 * 未登录返回结果
	 */
	public static ActionResult<Object> unauthorized(Object data) {
		ActionResult<Object> listResult = new ActionResult<>();
		listResult.setStatus(HttpStatus.UNAUTHORIZED.value());
		listResult.setMessage(HttpStatus.UNAUTHORIZED.name());
		listResult.setData(data);
		return listResult;
	}

	/**
	 * 未授权返回结果
	 */
	public static ActionResult<Object> forbidden(Object data) {
		ActionResult<Object> listResult = new ActionResult<>();
		listResult.setStatus(HttpStatus.FORBIDDEN.value());
		listResult.setMessage(HttpStatus.FORBIDDEN.name());
		listResult.setData(data);
		return listResult;
	}
}
